package ru.ipo.dces.utils;


import java.io.*;
import java.nio.charset.Charset;

/**
 * Created by IntelliJ IDEA.
 * User: Илья
 * Date: 18.12.2008
 * Time: 20:30:01
 */
public class FileSystemUtils {

    private FileSystemUtils() {
    }

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (String aChildren : children) {
                boolean success = deleteDir(new File(dir, aChildren));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }

    public static void ensureFileHasPath(File file) {
        if (!file.getParentFile().mkdirs())
            surprise();
    }

    private static void surprise() {
        //what a surprise!!!
    }

    public static boolean copyFile(File from, File to) {
        FileInputStream in = null;
        try {
            in = new FileInputStream(from);

            return copyFileFromInputStream(in, to);
        } catch (FileNotFoundException e) {
            return false;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    public static boolean saveBytesToFile(byte[] bytes, File to) {
        return copyFileFromInputStream(new ByteArrayInputStream(bytes), to);
    }

    public static byte[] loadBytesFromFile(File file) throws IOException {
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        return loadBytesFromStream(in);
    }

    public static byte[] loadBytesFromStream(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        copyStreams(in, out);
        return out.toByteArray();
    }

    public static boolean copyFileFromInputStream(InputStream in, File to) {
        try {
            FileOutputStream out = new FileOutputStream(to);

            copyStreams(in, out);

            out.close();
        } catch (IOException e) {
            return false;
        }

        return true;
    }

    public static void copyStreams(InputStream in, OutputStream out) throws IOException {
        final int BUFFER_SIZE = 4096;
        byte[] buffer = new byte[BUFFER_SIZE];

        int read;
        while ((read = in.read(buffer)) > 0)
            out.write(buffer, 0, read);
    }

    public static String inputStreamToString(InputStream is, Charset encoding) {
        InputStreamReader isr = new InputStreamReader(is, encoding);
        BufferedReader buff = new BufferedReader(isr);
        StringBuffer strBuff = new StringBuffer();
        int c;
        try {
            while ((c = buff.read()) != -1) {
                strBuff.append((char) c);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return strBuff.toString().trim();
    }

    public static String inputStreamToString(InputStream is) {
        return inputStreamToString(is, Charset.forName("UTF-8"));
    }
}
